<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table @row-dblclick="getClick" :data="dictionarytypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
      <el-table-column prop="id" label="编号" width="120" >
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="200" >
      </el-table-column>
      <el-table-column prop="sn" label="数据字典编号" width="200" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

    <!--新增或编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="字典名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--抽屉代码-->
    <el-drawer
        title="数据字典明细"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table @row-dblclick="getClick" :data="gridData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <!--这是elementui提供的选择框，此选择框可以多选-->
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column property="id" label="编号" width="150"></el-table-column>
        <el-table-column property="name" label="明细名" width="200"></el-table-column>

        <el-table-column label="操作" width="150">
          <template slot="header" slot-scope="scope">
            <el-button size="small" @click="detailhandleAdd(scope.$index, scope.row)">新增</el-button>
          </template>
          <template scope="scope">
            <el-button size="small" @click="detailhandleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="detailhandleDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--工具条-->
      <el-col :span="24" class="toolbar">
        <!--
          disabled：是否不可点击
            true：不可点击
            false：可点击
        -->
        <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
        <el-pagination layout="prev, pager, next" @current-change="detailhandleCurrentChange" :current-page="currentPageDrawer" :page-size="pageSizeDrawer" :total="totalDrawer" style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>

    <!--detail新增或编辑界面-->
    <el-dialog :title="title" :visible.sync="detaileditFormVisible" :close-on-click-modal="false">
      <el-form :model="detaileditForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="明细名" prop="name">
          <el-input v-model="detaileditForm.name" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="detaileditSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
        currentPageDrawer: 1,
        pageSizeDrawer: 5,
        totalDrawer: 0,
        detailTypesId: null,
        table: false,
				filters: {
					keyword: '',

				},
        typesId: null,
        title: "",
        gridData: [],
        departments: [],
				dictionarytypes: [],
        total: 0,
        currentPage: 1,
        pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
        detaileditFormVisible: false,//detail编辑界面是否显示
				editLoading: false,
				editFormRules: {
					username: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					],
          phone: [
						{ required: true, message: '请输入电话', trigger: 'blur' }
					],
          email: [
						{ required: true, message: '请输入邮箱', trigger: 'blur' }
					],
          age: [
						{ required: true, message: '请输入年龄', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id: null,
          name: '',
          sn: ''
				},
        detaileditForm: {
          id: null,
          name: '',
          typesId: null

				},



			}
		},
		methods: {
      getDictionarydetails(id) {
        let para = {
          currentPage: this.currentPageDrawer,
          pageSize: this.pageSizeDrawer,
          typesId: id
        };
        this.listLoading = true;
        //NProgress.start();
        this.$http.post("/dictionarydetail",para)
            .then(result=>{
              this.listLoading = false;
              this.detailTypesId = id,
              this.gridData = result.data.rows;
              console.log(result);
              this.totalDrawer = result.data.total;
            })
            .catch(result=>{
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
      },
      getClick(row){
        this.currentPageDrawer = 1;
        this.getDictionarydetails(row.id);
        this.table = true;

      },
      getDepartments(){
        this.$http.get("/department")
            .then(result=>{
              this.departments = result.data;
            })
      },
      search(){
        this.currentPage = 1;
        this.getDictionarytypes();
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionarytypes();
			},
      detailhandleCurrentChange(val) {
				this.currentPageDrawer = val;
				this.getDictionarydetails(this.detailTypesId);
			},
			//获取员工列表
			getDictionarytypes() {
        let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keyword: this.filters.keyword
        };
        this.listLoading = true;
        //NProgress.start();
        this.$http.post("/dictionarytype",para)
            .then(result=>{
              this.listLoading = false;
              this.dictionarytypes = result.data.rows;
              this.total = result.data.total;

            })
            .catch(result=>{
              this.$message({
                message: '系统繁忙，请重试！',
                type: 'error'
              });
            })
      },
      //删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.delete("/dictionarytype/"+ row.id)
              .then(result=>{
                this.listLoading = false;
                if (result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.currentPage = 1;
                this.getDictionarytypes();
              })
              .catch(result=>{
                this.listLoading = false;
                // 2.给出自定义失败提示！
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })
        }).catch(() => {
          /*这是点取消时会进入的*/
        });
      },
      //删除
      detailhandleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.delete("/dictionarydetail/"+ row.id)
              .then(result=>{
                this.listLoading = false;
                if (result.data.success){
                  this.$message({
                    message: result.data.message,
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: result.data.message,
                    type: 'error'
                  });
                }
                this.currentPage = 1;

                this.getDictionarydetails(this.detailTypesId);
              })
              .catch(result=>{
                this.listLoading = false;
                // 2.给出自定义失败提示！
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })
        }).catch(() => {
          /*这是点取消时会进入的*/
        });
      },
			//显示编辑界面
			handleEdit: function (index, row) {
        this.title = "编辑";
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},

      detailhandleEdit: function (index, row) {
        this.title = "编辑明细";
				this.detaileditFormVisible = true;
				this.detaileditForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.title = "新增";
        this.editFormVisible = true;
				this.editForm = {
          id: null,
          name: '',
          sn: ''
				};
			},
      detailhandleAdd: function () {
        this.title = "新增明细";
        this.detaileditFormVisible = true;
				this.detaileditForm = {
          id: null,
          name: '',
          typesId: this.detailTypesId
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.editForm);
              this.$http.put("/dictionarytype", para)
                  .then(result=>{
                    // 1.关闭提交按钮忙等
                    this.editLoading = false;
                    // 2.关闭弹出框
                    this.editFormVisible = false;
                    // 3.获取后端传递的AjaxResult对象
                    result = result.data;
                    // 4.判断删除是否成功
                    if (result.success){
                      // 删除成功
                      // 1.给出成功提示
                      this.$message({
                        message: result.message,
                        type: 'success'
                      });

                    }else {
                      // 删除失败
                      // 删除成功
                      // 1.给出失败提示
                      this.$message({
                        message: result.message,
                        type: 'error'
                      });

                    }
                    // 修改页码为第一页
                    this.currentPage = 1;
                    // 调用函数刷新列表
                    this.getDictionarytypes();
                  })
                  .catch(result=>{ // 接口调用失败
                    // 1.关闭忙等框
                    this.listLoading = false;
                    // 2.给出自定义失败提示！
                    this.$message({
                      message: '系统繁忙，请重试！',
                      type: 'error'
                    });
                  })
						});
					}
				});
			},
      /*detail新增和修改提交*/
      detaileditSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.detaileditForm);
              this.$http.put("/dictionarydetail", para)
                  .then(result=>{
                    // 1.关闭提交按钮忙等
                    this.editLoading = false;
                    // 2.关闭弹出框
                    this.detaileditFormVisible = false;
                    // 3.获取后端传递的AjaxResult对象
                    result = result.data;
                    // 4.判断删除是否成功
                    if (result.success){
                      // 删除成功
                      // 1.给出成功提示
                      this.$message({
                        message: result.message,
                        type: 'success'
                      });

                    }else {
                      // 删除失败
                      // 删除成功
                      // 1.给出失败提示
                      this.$message({
                        message: result.message,
                        type: 'error'
                      });

                    }
                    // 修改页码为第一页
                    this.currentPage = 1;
                    // 调用函数刷新列表
                    this.getDictionarydetails(this.detailTypesId);
                  })
                  .catch(result=>{ // 接口调用失败
                    // 1.关闭忙等框
                    this.listLoading = false;
                    // 2.给出自定义失败提示！
                    this.$message({
                      message: '系统繁忙，请重试！',
                      type: 'error'
                    });
                  })
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
      //批量删除
      batchRemove: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          this.$http.patch("/dictionarytype", ids)
              .then(result=>{
                // 1.获取后端传递的AjaxResult对象
                result = result.data;
                // 2.判断删除是否成功
                if (result.success){
                  // 删除成功
                  // 1.给出成功提示
                  this.$message({
                    message: result.message,
                    type: 'success'
                  });
                }else {
                  // 删除失败
                  // 删除成功
                  // 1.给出失败提示
                  this.$message({
                    message: result.message,
                    type: 'error'
                  });
                }
                // 修改页码为第一页
                this.currentPage=1;
                // 调用函数刷新列表
                this.getDictionarytypes();
              })
              .catch(result=>{ // 接口调用失败
                // 1.关闭忙等框
                this.listLoading = false;
                // 2.给出自定义失败提示！
                this.$message({
                  message: '系统繁忙，请重试！',
                  type: 'error'
                });
              })
        }).catch(() => {

        });
      }
    },
		mounted() {
			this.getDictionarytypes();

		}
	}

</script>

<style scoped>

</style>